﻿using System;
using System.Collections;
using System.Globalization;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Gmantis.Controls.Chart
{
    public class DataPoint
    {
        // Fields
        private DataSeries _ds;
        private object _item;
        private Keywords _kwords;
        private string _name;
        private string[] _names;
        private int _pi;
        private int _si;
        private double _val;
        internal Point LabelPoint;
        private static object NoValue = (double)1.0 / (double)0.0;
        internal Point Point;

        // Methods
        internal DataPoint(double value, object item)
        {
            this._si = -1;
            this._pi = -1;
            this._val = value;
            this._names = new string[] { "Value" };
            this._item = item;
        }

        internal DataPoint(DataSeries ds, int seriesIndex, int pointIndex, string[] names)
        {
            this._si = -1;
            this._pi = -1;
            this._ds = ds;
            this._si = seriesIndex;
            this._pi = pointIndex;
            this._names = names;
        }

        private void InitKeywords()
        {
            int num = (this._names != null) ? this._names.Length : 0;
            for (int i = 0; i < num; i++)
            {
                this.Keywords["#" + this._names[i]] = this.Series.GetValue(i, this.PointIndex);
            }
            this.Keywords["#YValue"] = this.Value;
            this.Keywords["#Value"] = this.Value;
            this.Keywords["#PointIndex"] = this.PointIndex;
            this.Keywords["#SeriesIndex"] = this.SeriesIndex;
            if (this._ds != null)
            {
                this.Keywords["#SeriesLabel"] = this._ds.Label;
            }
            this.Keywords["#NewLine"] = "\r\n";
        }

        public override string ToString()
        {
            if (!double.IsNaN(this.Value))
            {
                return this.Value.ToString();
            }
            return base.ToString();
        }

        // Properties
        public object DataObject
        {
            get
            {
                if (((this._item == null) && (this._ds != null)) && (this._pi >= 0))
                {
                    IEnumerable enumerable = this._ds.ItemsSource ?? ((this._ds.ParentData != null) ? this._ds.ParentData.ItemsSource : null);
                    if (enumerable != null)
                    {
                        IList list = enumerable as IList;
                        if (list != null)
                        {
                            if (this._pi < list.Count)
                            {
                                this._item = list[this._pi];
                            }
                        }
                        else
                        {
                            IEnumerator enumerator = enumerable.GetEnumerator();
                            DataUtils.TryReset(enumerator);
                            int num = 0;
                            while (enumerator.MoveNext())
                            {
                                if (num++ == this._pi)
                                {
                                    this._item = enumerator.Current;
                                    break;
                                }
                            }
                        }
                    }
                }
                return this._item;
            }
        }

        public object this[string name]
        {
            get
            {
                int length = this._names.Length;
                for (int i = 0; i < length; i++)
                {
                    if (name == this._names[i])
                    {
                        return this.Series.GetValue(i, this.PointIndex);
                    }
                }
                return NoValue;
            }
        }

        public object this[int index]
        {
            get
            {
                int length = this._names.Length;
                if (index < length)
                {
                    return this.Series.GetValue(index, this.PointIndex);
                }
                return NoValue;
            }
        }

        public object this[string name, string format]
        {
            get
            {
                int length = this._names.Length;
                for (int i = 0; i < length; i++)
                {
                    if (name == this._names[i])
                    {
                        IFormattable formattable = this.Series.GetValue(i, this.PointIndex) as IFormattable;
                        if (formattable != null)
                        {
                            return formattable.ToString(format, CultureInfo.CurrentCulture);
                        }
                    }
                }
                return NoValue;
            }
        }

        internal Keywords Keywords
        {
            get
            {
                if (this._kwords == null)
                {
                    this._kwords = new Keywords();
                    this.InitKeywords();
                }
                return this._kwords;
            }
        }

        public string Name
        {
            get
            {
                return this._name;
            }
            internal set
            {
                this._name = value;
            }
        }

        public double PercentagePoint
        {
            get
            {
                if (this._ds != null)
                {
                    return (this.Value / this._ds.GetPointSum(this.PointIndex));
                }
                return double.NaN;
            }
        }

        public double PercentageSeries
        {
            get
            {
                if (this._ds != null)
                {
                    return (this.Value / this._ds.GetSum());
                }
                return double.NaN;
            }
        }

        public int PointIndex
        {
            get
            {
                return this._pi;
            }
        }

        public DataSeries Series
        {
            get
            {
                return this._ds;
            }
        }

        public int SeriesIndex
        {
            get
            {
                return this._si;
            }
        }

        public double Value
        {
            get
            {
                if (this.Series == null)
                {
                    return this._val;
                }
                object obj2 = this.Series.GetValue(0, this.PointIndex);
                if (obj2 is double)
                {
                    return (double)obj2;
                }
                if (obj2 is DateTime)
                {
                    DateTime time = (DateTime)obj2;
                    return time.ToOADate();
                }
                return (double)NoValue;
            }
        }
    }
}
